home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / recio202.zip / rgetf.c < prev    next >
C/C++ Source or Header  |  1994-05-05  |  3KB  |  83 lines

  1. /*****************************************************************************
  2.    MODULE: rgetf.c
  3.   PURPOSE: recio character delimited floating point input functions
  4. COPYRIGHT: (C) 1994 William Pierpoint
  5.  COMPILER: Borland C Version 3.1
  6.        OS: MSDOS Version 6.2
  7.   VERSION: 2.02
  8.   RELEASE: May 5, 1994
  9. *****************************************************************************/
  10.  
  11. #include <ctype.h>
  12. #include <errno.h>
  13. #include <float.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17.  
  18. #include "recio.h"
  19.  
  20. extern int _rstatus(REC *rp, int mode);
  21. extern char *_rfldstr(REC *rp, size_t len);
  22. extern char *_rerrs(REC *rp, int errnum);
  23.  
  24. /****************************************************************************/
  25. static double                /* return floating point value                 */
  26.     _rgetd(                  /* get floating point number from rec stream   */
  27.         REC *rp,             /* pointer to record stream                    */
  28.         double negmin,       /* valid negative minimum value                */
  29.         double negmax,       /* valid negative maximum value                */
  30.         double posmin,       /* valid positive minimum value                */
  31.         double posmax)       /* valid positive maximum value                */
  32. /****************************************************************************/
  33. {
  34.     double result=0.0;       /* result to return */
  35.     double val;              /* conversion value */
  36.     char *fldptr;            /* pointer to field string */
  37.     char *endptr;            /* pointer to first invalid field char */
  38.  
  39.     if (!_rstatus(rp, R_READ)) { 
  40.       fldptr = _rfldstr(rp, 0); 
  41.       if (fldptr) { 
  42.         for (;;) { 
  43.           if (*fldptr != '\0') { 
  44.             endptr = fldptr; 
  45.             errno = 0; 
  46.             val = strtod(fldptr, &endptr); 
  47.             while (isspace(*endptr)) endptr++; 
  48.             if (errno==ERANGE || !*endptr) { 
  49.               if (!errno) { 
  50.                 if (!val || (val >= negmin && val <= negmax) || 
  51.                             (val >= posmin && val <= posmax)) { 
  52.                   result = val; 
  53.                   goto done; 
  54.                 } 
  55.               } /* data out of range */ 
  56.               fldptr = _rerrs(rp, R_ERANGE); 
  57.               if (fldptr) { continue; } else { goto done; } 
  58.             } /* invalid data */ 
  59.             fldptr = _rerrs(rp, R_EINVDAT); 
  60.             if (fldptr) { continue; } else { goto done; } 
  61.           } /* missing data */ 
  62.           fldptr = _rerrs(rp, R_EMISDAT); 
  63.           if (fldptr) { continue; } else { goto done; } 
  64.         } 
  65.       }
  66.     } 
  67. done: 
  68.     return result; 
  69. }
  70.  
  71. /****************************************************************************/
  72. /* character delimited floating point input functions                       */
  73. /****************************************************************************/
  74. float rgetf(REC *rp)
  75. {
  76.     return (float) _rgetd(rp, -FLT_MAX, -FLT_MIN, FLT_MIN, FLT_MAX);
  77. }
  78.  
  79. double rgetd(REC *rp)
  80. {
  81.     return _rgetd(rp, -DBL_MAX, -DBL_MIN, DBL_MIN, DBL_MAX);
  82. }
  83.